<?php
declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\Enterprise as EnterpriseModel;

/**
 * 企业认证
 * Class EnterpriseService
 * @package app\admin\service
 */
class EnterpriseService
{
    /**
     * 企业认证列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getEnterpriseList($where, $page, $pagesize)
    {
        $data = EnterpriseModel::getEnterpriseList($where, $page, $pagesize);

        foreach ($data as $k => $v) {
            $data[$k]['headimg'] = '<div><img class="headimgs" title="点我查看原图" onclick="lookimg(this.src)" src="' . $v['headimg'] . '" \' + \'alt=""></div><p>' . emojiDecode($v['nickname']) . ' / <span class="color3">' . $v['belong_position'] . '</span></p>';
            $data[$k]['remarks'] = '-';
            // 已驳回，获取最新原因
            if ($v['status'] == -1) {
                $data[$k]['remarks'] = EnterpriseRejectService::findEnterpriseRejectValue(['eid' => $v['id']]);
            }

            $data[$k]['cname'] = '<span class="mySpan">'.$v['cname'].'</span>';
        }

        return $data;
    }

    /**
     * 企业认证统计
     * @param $where
     * @return int
     */
    public static function countEnterpriseList($where)
    {
        return EnterpriseModel::countEnterpriseList($where);
    }

    /**
     * 所有企业认证
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getEnterprise()
    {
        return EnterpriseModel::getEnterprise();
    }

    /**
     * 指定企业认证
     * @param $where
     * @param int $assemble 0不组装  1组装
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findEnterprise($where, $assemble = 0)
    {
        $info = EnterpriseModel::findEnterprise($where);
        if (!$assemble) {
            return $info;
        }

        $info['qrcode'] = config('url.url') . $info['qrcode'];
        $info['logo'] = config('url.url') . $info['logo'];
        // 省份
        $info['province'] = LocationService::getLocationValue(['location_id' => $info['province']]);
        // 城市
        $info['city'] = LocationService::getLocationValue(['location_id' => $info['city']]);
        // 地区
        $info['area'] = LocationService::getLocationValue(['location_id' => $info['area']]);
        // 用户信息
        $users = UsersService::findUsers(['id' => $info['uid']]);
        $info['nickname'] = $users['nickname'];
        $info['headimg'] = $users['headimg'];
        // 行业
        $info['cname'] = IndustryCateService::findIndustryCateValue(['id' => $info['cid']]);
        // 企业图片资源
        $info['paths'] = EnterprisePathService::getEnterprisePath(['eid' => $info['id']]);

        return $info;
    }

    /**
     * 更新企业认证
     * @param $data
     * @param $where
     * @return EnterpriseModel
     */
    public static function updateEnterprise($data, $where)
    {
        return EnterpriseModel::updateEnterprise($data, $where);
    }

    /**
     * 指定企业认证字段值
     * @param $where
     * @param string $field
     * @return mixed
     */
    public static function findEnterpriseValue($where, $field = 'name')
    {
        return EnterpriseModel::findEnterpriseValue($where, $field);
    }

    /**
     * 创建企业认证
     * @param $data
     * @return EnterpriseModel
     */
    public static function createEnterprise($data)
    {
        return EnterpriseModel::createEnterprise($data);
    }
}